package com.howtodoinjava.controller;

import java.io.IOException;
import java.io.InputStream;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.howtodoinjava.security.MyUserContext;

@Controller
public class MainPageController {

	@Autowired
	MyUserContext myUserContext;

	@RequestMapping(value = "/", method = RequestMethod.GET)
	public @ResponseBody String getHomePage(ModelMap model) {
		return "homePage";
	}

	@RequestMapping(value = "/aboutUs", method = RequestMethod.GET)
	public String getAboutUs(ModelMap model) {
		return "aboutUsPage";
	}

	@RequestMapping(value = "/services", method = RequestMethod.GET)
	public String getServices(ModelMap model) {
		return "servicesPage";
	}

	@RequestMapping(value = "/networks", method = RequestMethod.GET)
	public String getNetworks(ModelMap model) {
		return "networkPage";
	}

	@RequestMapping(value = "/contact", method = RequestMethod.GET)
	public String getContact(ModelMap model) {
		return "contactPage";
	}

	@RequestMapping(value = "/testPoi", method = RequestMethod.GET)
	public String getUploadPage(ModelMap model) {
		model.put("fileName", "springhibernate_db.sql");
		return "testPoi";
	}

	@RequestMapping(value = "/testPoi/download/{path:.*}", method = RequestMethod.GET)
	public void downloadTemplate(ModelMap model,
			@PathVariable("path") String path, HttpServletResponse response) {
		try {
			System.out.println("Dowload file name : " + path);

			// get your file as InputStream
			ClassPathResource cpr = new ClassPathResource(path);
			InputStream is = cpr.getInputStream();
			// copy it to response's OutputStream
			IOUtils.copy(is, response.getOutputStream());
			response.setContentType("application/octet-stream");
			response.setHeader("Content-Disposition", "attachment;filename="
					+ path);
			response.flushBuffer();
		} catch (IOException ex) {
			throw new RuntimeException("IOError writing file to output stream");
		}
	}

	@RequestMapping(value = "/contact/save", method = RequestMethod.POST)
	public @ResponseBody String saveContact(
			@RequestParam(value = "name") String name,
			@RequestParam(value = "email") String email,
			@RequestParam(value = "subject") String subject,
			@RequestParam(value = "content") String content, ModelMap model)
			throws Exception {
		StringBuffer sb = new StringBuffer();
		sb.append(name);
		sb.append("/");
		sb.append(email);
		sb.append(" Wanna contact you. Here is content: ");
		sb.append(content);
		// MailerUtils.Send("henrycuongduy@gmail.com", "", subject,
		// sb.toString());
		return "";
	}
}
